package v1

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"net/http"
	"server/common"
	"server/internal"
	"server/internal/model"
	"server/pkg/app"
)

func CreatePipeline(c *gin.Context) {
	userinfo := GetUser(c)
	_ = c.Request.ParseForm()
	pipe := &model.PipeApi{}
	_ = c.ShouldBind(pipe)
	group_str, err := json.Marshal(pipe.TaskGroups)
	if err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	pipe.GroupStr = group_str
	pipe.OwnerID = userinfo.ID
	err = pipe.Create()
	if err != nil {
		c.String(http.StatusOK, err.Error())
		go CreateLog(c, "流水线", "创建流水线"+pipe.Name+"失败："+err.Error())
		return
	}
	c.String(http.StatusOK, "流水线创建成功")
	go CreateLog(c, "流水线", "创建流水线"+pipe.Name)
}

func QueryPipeInfo(c *gin.Context) {
	pipename := c.Param("name") //获取url路径中参数
	pipe := &model.PipeApi{}
	pipe.Name = pipename
	pipe.InfoOmit()

	err := json.Unmarshal([]byte(pipe.GroupStr), &pipe.TaskGroups)
	if err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": pipe,
	})
}

func ListPipeline(c *gin.Context) {
	pages := &app.Page{}
	pages.Info(c)
	pipe := &model.PipeLine{}
	pipe_list, total := pipe.List(pages)
	c.JSON(http.StatusOK, gin.H{
		"total": total,
		"data":  pipe_list,
	})
}

func UpdatePippipe(c *gin.Context) {
	userinfo := GetUser(c)
	_ = c.Request.ParseForm()
	pipe := &model.PipeApi{}
	_ = c.ShouldBind(pipe)
	group_str, err := json.Marshal(pipe.TaskGroups)
	if err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	pipe.GroupStr = group_str
	pipe.OwnerID = userinfo.ID
	err = pipe.Update()
	if err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.String(http.StatusOK, "流水线更新成功")
	go CreateLog(c, "流水线", "修改流水线："+pipe.Name)
}

func DeletePipeline(c *gin.Context) {
	userinfo := GetUser(c)
	name := c.Param("name") //获取url路径中参数
	pipe := &model.PipeLine{}
	pipe.Name = name
	pipe.Info()
	if pipe.Owner.Name != userinfo.Name {
		c.JSON(http.StatusOK, gin.H{
			"msg": "无权限",
		})
		go CreateLog(c, "流水线", "删除流水线"+pipe.Name+"失败："+"无权限")
		return
	}
	Delete(pipe, c)
	go CreateLog(c, "流水线", "删除流水线："+pipe.Name)
}

func CopyPipeline(c *gin.Context) {
	userinfo := GetUser(c)
	_ = c.Request.ParseForm()
	cy := internal.Copy{}
	_ = c.ShouldBind(cy)

	pipe := &model.PipeLine{}
	pipe.Name = cy.Name
	pipe.Info()
	pipe.Name = cy.NewName
	pipe.OwnerID = userinfo.ID
	err := pipe.Create()
	if err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.String(http.StatusOK, "流水线创建成功")
}

func StartPipeline(c *gin.Context) {
	userinfo := GetUser(c)
	_ = c.Request.ParseForm()
	pipe := &model.PipeLine{}
	_ = c.ShouldBind(pipe)
	pipe.Info()

	hispipe := &model.HisPipe{}
	hispipe.OwnerID = userinfo.ID
	hispipe.PipeLineID = pipe.ID
	hispipe.Status = common.RUNNING
	err := hispipe.Create()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg": err.Error(),
		})
	} else {
		go CreateLog(c, "流水线", "运行流水线："+pipe.Name)
		pipe.Status = common.RUNNING
		err = pipe.UpdateStaus()
		if err != nil {
			go CreateLog(c, "流水线", "更新流水线 "+err.Error())
		}
		c.String(http.StatusOK, "流水线"+pipe.Name+"运行成功")
	}

}

func StopPipeline(c *gin.Context) {
	_ = c.Request.ParseForm()
	pipe := &model.PipeLine{}
	_ = c.ShouldBind(pipe)
	pipe.Status = common.STOPPED
	err := pipe.Update()
	if err != nil {
		c.String(http.StatusOK, "停止任务失败")
		return
	}
	c.String(http.StatusOK, "停止任务成功")
	go CreateLog(c, "流水线", "停止流水线："+pipe.Name)
}

// 流水线定时任务开关
func PipeSwitch(c *gin.Context) {
	userinfo := GetUser(c)
	_ = c.Request.ParseForm()
	pipe := &model.PipeLine{}
	_ = c.ShouldBind(pipe)
	pipe.OwnerID = userinfo.ID
	err := pipe.SwitchCron()
	if err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	if pipe.Switch == false {
		c.String(http.StatusOK, "关闭定时任务成功")
		go CreateLog(c, "流水线", "关闭流水线定时任务："+pipe.Name)
	} else {
		c.String(http.StatusOK, "开启定时任务成功")
		go CreateLog(c, "流水线", "开启流水线定时任务："+pipe.Name)
	}
}
